package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.Response;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ScmStockMoveItemParam;
import com.xinsoft.entity.dto.ScmStockMoveParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.entity.vo.ScmStockMoveItemVo;
import com.xinsoft.entity.vo.ScmStockMovePageVo;
import com.xinsoft.entity.vo.ScmStockMoveVo;
import com.xinsoft.service.*;
import com.xinsoft.mapper.ScmStockMoveMapper;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 13370
 * @description 针对表【scm_stock_move(库存调拨表)】的数据库操作Service实现
 * @createDate 2023-08-17 09:52:00
 */
@Service
public class ScmStockMoveServiceImpl extends ServiceImpl<ScmStockMoveMapper, ScmStockMove>
        implements ScmStockMoveService {

    @Resource
    private ScmStockMoveMapper stockMoveMapper;
    @Autowired
    private ScmStockMoveItemService stockMoveItemService;
    @Autowired
    private SysDocumentNumService sysDocumentNumService;
    @Autowired
    private ScmMoveInstockService moveInstockService;
    @Autowired
    private ScmMoveInstockItemService moveInstockItemService;
    @Autowired
    private ScmMoveOutstockService moveOutstockService;
    @Autowired
    private ScmMoveOutstockItemService moveOutstockItemService;

    @Override
    public Response<?> audit(Integer id) {
        ScmStockMove scmStockMove = this.baseMapper.selectById(id);
        if (Const.AUDIT_STATUS_NORMAL.equals(scmStockMove.getAuditStatus())) {
            return Response.fail("该单据已审核！");
        }
        scmStockMove.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
        scmStockMove.setAuditId(TokenUtil.getUserId());
        scmStockMove.setAuditTime(new Date());
        this.baseMapper.updateById(scmStockMove);

        // 1.生成调拨出库和调拨入库
        // 2.调拨出库扣库存、调拨入库加库存
        List<ScmStockMoveItem> itemList = stockMoveItemService.list(Wrappers.lambdaQuery(ScmStockMoveItem.class).eq(ScmStockMoveItem::getMoveId, id));
        this.saveMoveOutstock(scmStockMove, itemList);
        this.saveMoveInstock(scmStockMove, itemList);
        return Response.succeed();
    }

    @Override
    public Response<?> reverseAudit(Integer id) {
        ScmStockMove scmStockMove = this.baseMapper.selectById(id);
        if (Const.AUDIT_STATUS_WAIT.equals(scmStockMove.getAuditStatus())) {
            return Response.fail("该单据未审核，不能反审核！");
        }
        int instockCiteCount = stockMoveMapper.getMoveInstockCiteCount(id);
        int outstockCiteCount = stockMoveMapper.getMoveOutstockCiteCount(id);
        if (instockCiteCount > 0 || outstockCiteCount > 0) {
            throw new BDException("该单据已产生下游单据，不能反审核");
        }
        scmStockMove.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        scmStockMove.setAuditId(null);
        scmStockMove.setAuditTime(null);
        this.baseMapper.updateById(scmStockMove);
        return Response.succeed();

    }

    @Override
    public Response<?> findPageListByParam(ScmStockMoveParam param) {
        Page<ScmStockMovePageVo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<ScmStockMovePageVo> pageParamList = this.baseMapper.findPageListByParam(page, param);
        return Response.succeed(pageParamList);
    }

    @Override
    public ScmStockMoveVo detail(Integer id) {
        ScmStockMoveVo detailVO = this.baseMapper.getDetail(id);
        ScmStockMoveItemParam itemParam = new ScmStockMoveItemParam();
        itemParam.setMoveId(id);
        List<ScmStockMoveItemVo> itemList = stockMoveItemService.listPurchaseItem(itemParam);
        detailVO.setItemList(itemList);
        return detailVO;
    }

    @Override
    public List<ScmStockMovePageVo> findListByParam(ScmStockMoveParam param) {
        List<ScmStockMovePageVo> pageListByParam = this.baseMapper.findPageListByParam(param);
        return pageListByParam;
    }

    /**
     * 保存调拨入库并审核
     *
     * @param scmStockMove 调拨方案
     * @param itemList     调拨方案明细
     */
    public ScmMoveInstock saveMoveInstock(ScmStockMove scmStockMove, List<ScmStockMoveItem> itemList) {
        ScmMoveInstock instock = new ScmMoveInstock();
        instock.setInstockCode(sysDocumentNumService.getSysDocumentNumByDocumentId(Const.DOCUMENT_ID.MOVE_INSTOCK_DOCUMENT));
        instock.setInstockPersonId(TokenUtil.getUserId());
        instock.setInstockDate(new Date());
        moveInstockService.save(instock);
        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.MOVE_INSTOCK_DOCUMENT);

        List<ScmMoveInstockItem> instockItemList = new ArrayList<>();
        itemList.forEach(item -> {
            ScmMoveInstockItem instockItem = new ScmMoveInstockItem();
            instockItem.setMoveInstockId(instock.getId());
            instockItem.setMaterialId(item.getMaterialId());
            instockItem.setInstockNum(item.getMoveNum());
            instockItem.setAssistUom(item.getAssistUom());
            instockItem.setInstockassistNum(item.getMoveNum().add(item.getConvCoefficient()));
            instockItem.setConvCoefficient(item.getConvCoefficient());
            instockItem.setBatch(item.getMoveInBatch());
            instockItem.setWarehouseId(item.getWarehInId());
            instockItem.setWarehItemId(item.getWarehInitemId());

            instockItem.setSourceId(scmStockMove.getId());
            instockItem.setSourceType(Const.DOCUMENT_ID.STOCK_MOVE_DOCUMENT);
            instockItem.setSourceCode(scmStockMove.getMoveCode());
            instockItemList.add(instockItem);
        });
        moveInstockItemService.saveBatch(instockItemList);
        // 审核
        moveInstockService.audit(instock.getId());
        return instock;
    }

    /**
     * 保存调拨出库并审核
     *
     * @param scmStockMove 调拨方案
     * @param itemList     调拨方案明细
     */
    public ScmMoveOutstock saveMoveOutstock(ScmStockMove scmStockMove, List<ScmStockMoveItem> itemList) {
        ScmMoveOutstock outstock = new ScmMoveOutstock();
        outstock.setOutstockCode(sysDocumentNumService.getSysDocumentNumByDocumentId(Const.DOCUMENT_ID.MOVE_OUTSTOCK_DOCUMENT));
        outstock.setOutstockPersonId(TokenUtil.getUserId());
        outstock.setOutstockTime(new Date());
        moveOutstockService.save(outstock);
        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.MOVE_OUTSTOCK_DOCUMENT);
        List<ScmMoveOutstockItem> outstockItemList = new ArrayList<>();
        itemList.forEach(item -> {
            ScmMoveOutstockItem outstockItem = new ScmMoveOutstockItem();
            outstockItem.setOutstockId(outstock.getId());
            outstockItem.setMaterialId(item.getMaterialId());
            outstockItem.setOutstockNum(item.getMoveNum());
            outstockItem.setAssistUom(item.getAssistUom());
            outstockItem.setConvCoefficient(item.getConvCoefficient());
            outstockItem.setBatch(item.getMoveOutBatch());
            outstockItem.setWarehouseId(item.getWarehOutId());
            outstockItem.setWarehItemId(item.getWarehOutitemId());

            outstockItem.setSourceId(scmStockMove.getId());
            outstockItem.setSourceType(Const.DOCUMENT_ID.STOCK_MOVE_DOCUMENT);
            outstockItem.setSourceCode(scmStockMove.getMoveCode());
            outstockItemList.add(outstockItem);
        });
        moveOutstockItemService.saveBatch(outstockItemList);
        // 审核
        moveOutstockService.audit(outstock.getId());
        return outstock;
    }
}




